﻿// ***********************************************************************
// Assembly         : SecurityLayer
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="ProviderUtils.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Linq.Expressions;
using InnovaApp.Layers.CommonLayer;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.DomainLayer;
using INNOVACALL.Framework.Common;

namespace InnovaApp.Layers.SecurityLayer.RoleAndMembershipProvider
{
    /// <summary>
    /// Class ProviderUtils
    /// </summary>
	internal class ProviderUtils : _RoleAndMembershipProvider_Base
	{
        /// <summary>
        /// A helper function to retrieve config values from the configuration file.
        /// </summary>
        /// <param name="config">Provider configuration.</param>
        /// <param name="configKey">Key of the configuration that should be read.</param>
        /// <param name="defaultValue">Default value being used if the config does not exist.</param>
        /// <returns>Configuration value or default value if not exisiting.</returns>
		public static object GetConfigValue(NameValueCollection config, string configKey, object defaultValue)
		{
			object configValue;

			try
			{
				configValue = defaultValue;
				configValue = config[configKey];
				if (configValue != null)
				{
					configValue = string.IsNullOrEmpty(configValue.ToString()) ? defaultValue : configValue;
				}
			}
			catch
			{
				configValue = defaultValue;
			}

			return configValue;
		}

        /// <summary>
        /// Ensure that application exists. If not -&gt; create new application.
        /// </summary>
        /// <param name="applicationName">Application name.</param>
        /// <param name="_context">Entity Framework data context.</param>
        /// <returns>The application object</returns>
		public static InnoApplication EnsureApplication(string applicationName, DataRepositories _context)
		{
			InnoApplication application = _context.InnoApplication_Rep.Query(a => a.Name == applicationName).FirstOrDefault();
			if (application == null)
			{
				// Create application
				application = new InnoApplication() { Id = Guid.NewGuid(), Name = applicationName, Description = string.Empty, FK_Language = EnumTools.EnumTypeTo<int>(enLanguages.en) };
				_context.InnoApplication_Rep.Add(application);
				_context.Save();
			}

			return application;
		}

        /// <summary>
        /// Builds a contains expression.
        /// </summary>
        /// <typeparam name="TElement">The type of the element.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="valueSelector">The value selector.</param>
        /// <param name="values">The values.</param>
        /// <returns>Expression{Func{``0System.Boolean}}.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// valueSelector
        /// or
        /// values
        /// </exception>
		public static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
		{
			if (null == valueSelector)
			{
				throw new ArgumentNullException("valueSelector");
			}

			if (null == values)
			{
				throw new ArgumentNullException("values");
			}

			ParameterExpression p = valueSelector.Parameters.Single();

			if (!values.Any())
			{
				return e => false;
			}

			IEnumerable<Expression> equals = values.Select(value => (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue))));
			Expression body = equals.Aggregate(Expression.Or);
			return Expression.Lambda<Func<TElement, bool>>(body, p);
		}
	}
}
